package com.fbinggun.fattribute.attribute.other;

import com.fbinggun.fattribute.FAttributePlugin;
import com.fbinggun.fattribute.attribute.AOther;
import com.fbinggun.fattribute.attribute.IAttribute;
import com.fbinggun.fattribute.data.DataManage;
import com.fbinggun.fattribute.data.EntityData;
import com.fbinggun.fbinggunutil.RandomUtil;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.EntityDamageByEntityEvent;

import java.util.HashMap;
import java.util.Map;

public class Crit extends AOther {

    @Override
    public Map<IAttribute, Double> modifyDamage(EntityData entityData, Map<IAttribute, Double> map, EntityDamageByEntityEvent event) {
        if(event.getEntity() instanceof LivingEntity) {
            EntityData damageEntityData = DataManage.getData((LivingEntity) event.getEntity());
            double crit=getValue(entityData)*0.01;
            crit=crit-damageEntityData.getValue(FAttributePlugin.plugin.getConfigKey("CritDefense"))*0.01;
            crit=  Math.max(0.0001,crit);
                if(RandomUtil.is(crit)){
                    Map<IAttribute, Double> list = new HashMap<>();
                    double rate = 0.5+entityData.getValue(FAttributePlugin.plugin.getConfigKey("CritRate"))*0.01;
                   for(IAttribute iAttribute:map.keySet()){
                            double damage=map.get(iAttribute)+map.get(iAttribute)*rate;
                       list.put(iAttribute,damage);
                   }
                   double d=event.getDamage()+event.getDamage()*rate;
                    event.setDamage(d);
                    return list;
                }

        }
        return map;
    }

    @Override
    public String getKey() {
        return FAttributePlugin.plugin.getConfigKey("Crit");
    }

    @Override
    public String getID() {
        return "Crit";
    }

    @Override
    public int getCombat() {
        return 20;
    }
}
